package com.saianfu.flowable.demo.common.impl

import com.saianfu.flowable.demo.common.ActTask
import org.flowable.engine.TaskService
import org.flowable.engine.task.Comment
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import java.util.*

/**
 * 流程任务
 */
@Component
class TaskHandler : ActTask {

    @Autowired
    lateinit var taskService: TaskService

    @Autowired
    lateinit var histTaskQueryHandler: HistTaskQueryHandler

    @Throws(RuntimeException::class, Exception::class)
    override fun claim(taskId: String, userId: String) {
        taskService.claim(taskId, userId)
    }

    @Throws(RuntimeException::class, Exception::class)
    override fun unclaim(taskId: String) {
        taskService.unclaim(taskId)
    }

    @Throws(RuntimeException::class, Exception::class)
    override fun complete(taskId: String) {
        this.complete(taskId, null)
        logger.info("-----------任务ID：{},已完成-----------", taskId)
    }

    @Throws(RuntimeException::class, Exception::class)
    override fun complete(taskId: String, variables: Map<String, Any?>?) {
        taskService.complete(taskId, variables)
    }

    override fun complete(taskId: String, variables: Map<String, Any?>?, localScope: Boolean): Map<String, Any?> {
        val finishTask = histTaskQueryHandler!!.finishedTask(taskId)
        taskService.complete(taskId, variables, localScope)
        val activeTask = histTaskQueryHandler.activeTask(finishTask.processInstanceId)
        val map: MutableMap<String, Any?> = HashMap(16)
        map["finish"] = finishTask
        map["active"] = activeTask
        return map
    }

    override fun delegate(taskId: String, userId: String?) {
        taskService.delegateTask(taskId, userId)
    }

    override fun resolveTask(taskId: String) {
        taskService.resolveTask(taskId)
    }

    override fun setAssignee(taskId: String, userId: String?) {
        taskService.setAssignee(taskId, userId)
    }

    override fun setOwner(taskId: String, userId: String?) {
        taskService.setOwner(taskId, userId)
    }

    override fun delete(taskId: String) {
        taskService.deleteTask(taskId)
    }

    override fun deleteWithReason(taskId: String, reason: String?) {
        taskService.deleteTask(taskId, reason)
    }

    @Throws(RuntimeException::class, Exception::class)
    override fun addCandidateUser(taskId: String, userId: String?) {
        taskService.addCandidateUser(taskId, userId)
    }

    @Throws(RuntimeException::class, Exception::class)
    override fun addComment(taskId: String, processInstanceId: String, message: String?): Comment {
        return taskService.addComment(taskId, processInstanceId, message)
    }

    @Throws(RuntimeException::class, Exception::class)
    override fun getTaskComments(taskId: String): List<Comment> {
        return taskService.getTaskComments(taskId)
    }

    override fun setVariableLocal(taskId: String, variableName: String, value: Any?) {
        taskService.setVariableLocal(taskId, variableName, value)
    }

    override fun setVariablesLocal(taskId: String, variables: Map<String, Any?>) {
        taskService.setVariablesLocal(taskId, variables)
    }

    companion object {
        protected var logger = LoggerFactory.getLogger(TaskHandler::class.java)
    }
}